package com.snatch.data.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.snatch.common.constant.SnatchConstant;
import com.snatch.common.utils.DateUtil;
import com.snatch.data.service.OddsService;
import com.snatch.data.vo.Item;
import com.snatch.data.vo.Match;
import com.snatch.data.vo.Play;
import com.snatch.data.vo.Root;
import com.snatch.data.vo.Sports;
import com.snatch.snatchmgr.entity.db.ThirdLive;
import com.snatch.snatchmgr.entity.db.ThirdMatch;
import com.snatch.snatchmgr.entity.db.ThirdPlayinfo;
import com.snatch.snatchmgr.entity.db.ThirdTradingItem;
import com.snatch.snatchmgr.system.SystemData;
import com.snatch.snatchmgr.util.ToXmlUtil;


@Service
public class OddsServiceImpl implements OddsService {
	
	public String oddsData(String webCode,String sportsCode,String gameType,String lastTime){
		Sports sports = findSports(webCode, sportsCode,gameType, lastTime);
		Root root = new Root();
		root.setSuccess(SnatchConstant.SUCCESS);
		root.setSportsCode(sportsCode);
		root.setSource(webCode);
		return ToXmlUtil.rootToXml(root,sports);
	}
	

	@Override
	public String liveData(String webCode, String sportsCode,String lastTime) {
		Root root = new Root();
		root.setSuccess(SnatchConstant.SUCCESS);
		root.setSportsCode(sportsCode);
		root.setSource(webCode);
		List<ThirdLive> list = new ArrayList<ThirdLive>();
		
		Map <String, ThirdLive> map = SystemData.findLiveMap(webCode,sportsCode);
		if(null != map){
			Iterator<ThirdLive> iterator = map.values().iterator();
			while (iterator.hasNext()) {
				ThirdLive liveData = iterator.next();
				list.add(liveData.clone());
			}
		}
		return ToXmlUtil.liveToXml(root, list);
	}
	
	
	private Sports findSports(String webCode,String sportsCode,String gameType,String lastTime){
		Timestamp startTime = null;
		if ("".equals(lastTime)) {
			startTime = Timestamp.valueOf("1970-01-01 00:00:00.0");
		} else {
			startTime = Timestamp.valueOf(lastTime);
		}
		Sports sports = new Sports();
		sports.setName(sportsCode);
		Map<String, ThirdMatch> thirdMatchmap = SystemData.findThirdMatchs(webCode,sportsCode,gameType);
		if(null != thirdMatchmap){
			Iterator<ThirdMatch> mapchIterator = thirdMatchmap.values().iterator();
			while (mapchIterator.hasNext()) {
				ThirdMatch thirdMatch = mapchIterator.next();
				if(thirdMatch.getChangeTime()>=startTime.getTime()){
					sports.getMatchs().add(getMatchByThirdMatch(thirdMatch, startTime));
				}
			}
		}
		return sports;
	}
	
	
	public Match getMatchByThirdMatch(ThirdMatch thirdMatch,Timestamp startTime){
		Match match = new Match();
		match.setLeagueSelfID(thirdMatch.getLeagueSelfid());
		match.setLeagueName(thirdMatch.getLeagueName());
		match.setMatchSelfID(thirdMatch.getMatchSelfid());
		match.setMatchName(thirdMatch.getMatchName());
		match.setMatchType(thirdMatch.getMatchType());
		match.setTarget(thirdMatch.getTarget());
		match.setStartTime(DateUtil.format(thirdMatch.getStartTime(), SnatchConstant.MATCH_START_TIME));
		match.setMatchType(thirdMatch.getMatchType());
		match.setBunch(thirdMatch.isBunch());
		match.setBunchNumber(thirdMatch.getBunchNumber());
		match.setValid(thirdMatch.isValid());
		if(thirdMatch.isValid()){
			Iterator<ThirdPlayinfo> playIterator = thirdMatch.getThirdPlayinfos().values().iterator();
			while (playIterator.hasNext()) {
				ThirdPlayinfo thirdPlayinfo = playIterator.next();
				if(thirdPlayinfo.getChangeTime()>=startTime.getTime()){
					match.getPlays().add(getPlayByThirdPlayinfo(thirdPlayinfo, startTime));
				}
			}
		}
		
		
		return match;
	}
	
	public Play getPlayByThirdPlayinfo(ThirdPlayinfo thirdPlayinfo,Timestamp startTime){
		Play play = new Play();
		play.setName(thirdPlayinfo.getName());
		play.setGametime(thirdPlayinfo.getGametime());
		play.setBunch(thirdPlayinfo.isBunch());
		play.setValid(thirdPlayinfo.isValid());
		play.setHandicap(thirdPlayinfo.getHandicap());
		if(thirdPlayinfo.isValid()){
			Iterator<ThirdTradingItem> thirdTradingItems = thirdPlayinfo.getThirdTradingItems().values().iterator();
			while (thirdTradingItems.hasNext()) {
				ThirdTradingItem thirdTradingItem = thirdTradingItems.next();
				Item item = new Item();
				item.setName(thirdTradingItem.getName());
				item.setRatio(thirdTradingItem.getRatio());
				play.getItems().add(item);
			}
		}
		return play;
	}


}
